/**
 * 乘客行程编辑、新增
 *
 */
define(["app", "js/services/MessageBox", "js/services/MuseService"
    , "js/controllers/trip/trip/TripCommon.js"
    , "js/controllers/trip/trip/TripActivitySelectController.js"], function (app) {

    var deps = ["$scope", "$state", "$modal", "$stateParams", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, $stateParams, MessageBox, MuseService) {
        //获取参数
        var id = $stateParams.id || '';

        //获取数据
        $scope.getDetail = function () {
            MuseService.get(baseServices_restfulUrl + "trip/load.json?id=" + id).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.trip = data.data;
                    } else {
                        MessageBox.notify('获取数据失败:' + data.message);
                    }
                });
        };
        if (id) {
            $scope.getDetail();
        }

        //保存
        $scope.saveObj = function () {
            MuseService.postObj(baseServices_restfulUrl + "trip/save.json", $scope.trip).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.result = data.data;
                        MessageBox.notify('保存成功!');
                        $state.go('tripList');
                    } else {
                        MessageBox.notify('保存失败：' + data.message);
                    }
                });
        };

        // 取消，返回
        $scope.close = function () {
            //$state.go("tripList");
            history.back();
        };


        var onSelectRouteSiteFun = function (routeSite) {

            var routeSiteEntity = {
                name: routeSite.name,
                routeSite: routeSite,
                route: {
                    id: $scope.route.id
                },
                sort: $scope.route.sites.length,
                cost: 0
            };

            //选择site
            console.log("选择了" + routeSite);
            var added = $scope.route.sites.indexOf(routeSiteEntity, function (a, b) {
                    return a.routeSite.id == b.routeSite.id;
                }) > -1;
            if (!added) {
                $scope.route.sites.push(routeSiteEntity);
            }
        }

        //流转订单给司机
        $scope.flowTripToDriver = function () {

            $modal.open({
                templateUrl: 'views/trip/trip/tripActivitySelect.html',
                controller: 'TripActivitySelectController',
                backdrop: 'static',
                keyboard: false,
                size: 'lg',
                resolve: {
                    trip: function () {
                        return angular.copy($scope.trip);
                    }
                },
                windowClass: 'app-modal-window'
            }).result.then(function (result) {
                if (result == "tripFlowSuccess") {
                    $scope.getDetail();
                    console.log("流转成功，重新加载数据");
                }
            });
        };

        //保存强制修改状态
        $scope.forceChangeSatate = function () {

            var dialog = MessageBox.confirm("你确定需要强制改变这个乘客行程乘客行程的状态？这样可能会带来无法预料的后果。");
            dialog.result.then(function () {
                var param = {
                    tripId: $scope.trip.id,
                    state: $scope.trip.state
                }

                MuseService.post(baseServices_restfulUrl + "trip/forceChangeSatate.json", param).success(
                    function (data, status, headers, config) {
                        if (data.state == 0) {
                            MessageBox.notify('修改成功!');
                            $scope.getDetail();
                        } else {
                            MessageBox.notify('修改失败：' + data.message);
                        }
                    })
                .error(function () {
                    MessageBox.error('服务器正在开小差zZZ,修改失败');
                });
            });

        };

        //保存强制修改状态
        $scope.cancelTrip = function () {

            var dialog = MessageBox.confirm("确定取消当前乘客行程?此操作会强制取消当前乘客行程，即使乘客已经支付完成。");
            dialog.result.then(function () {
                var param = {
                    tripId: $scope.trip.id,
                    reason: '系统管理员后台取消'
                }

                MuseService.post(baseServices_restfulUrl + "trip/cancelTrip.json", param).success(
                    function (data, status, headers, config) {
                        if (data.state == 0) {
                            MessageBox.notify('取消乘客行程成功!');
                            $scope.getDetail();
                        } else {
                            MessageBox.notify('取消乘客行程失败：' + data.message);
                        }
                    })
                .error(function () {
                    MessageBox.error('服务器正在开小差zZZ,修改失败');
                });
            });

        };
    }

    controller.$inject = deps;
    app.lazy.controller("TripEditController", controller);
});
